using System; 
using Server.Items;
using Server.Targeting;
using Server.Mobiles;
using Server.Gumps;
using System.Collections;

namespace Server.Items
{
    public class PortaPetRes : Item
    {
        private int m_Charges;

	[CommandProperty(AccessLevel.Administrator)]
        public int Charges
        {
            get { return m_Charges; }
            set
            {
                m_Charges = value;
                InvalidateProperties();
		if ( m_Charges <= 0 )
			this.Delete();
            }
        }
   
      [Constructable] 
      public PortaPetRes() : base( 3985 ) 
      {
          Name = "a Portable Pet Res Stone";
          Movable = true;
          LootType = LootType.Blessed;
          Charges = 5;
                    
      } 
      
      public PortaPetRes( Serial serial ) : base( serial )
      {
      
      } //Below
      public override void GetProperties(ObjectPropertyList list)
      {
          base.GetProperties(list);
          if (Charges > 0)
              list.Add("Charges: {0}", Charges);
      }
      public override void Serialize( GenericWriter writer ) //Above
      { 
         base.Serialize( writer ); 

         writer.Write( (int) 0 ); // version 
         writer.Write((int)m_Charges);//ADDED
      } 

      public override void Deserialize( GenericReader reader ) 
      { 
         base.Deserialize( reader ); 

         int version = reader.ReadInt();
         m_Charges = reader.ReadInt();//ADDED
      } 

      public override void OnDoubleClick( Mobile m )
      {      
			if ( !IsChildOf (m.Backpack))
			{
			m.SendMessage( 77, "must be in your Backpack!" );
			}
			else
			{
				m.RevealingAction();

				m.SendMessage( "What pet would you like to resurrect?" );

				m.Target = new PetResTarget();
                --this.Charges;
			}
		}

	private class PetResTarget : Target
	{

		public PetResTarget( ) : base( 1, false, TargetFlags.Beneficial )
		{
	}	
	            
        protected override void OnTarget( Mobile m, object obj )
        {
      	  if(obj is Mobile)
      		  { 
         	  Mobile mob = (Mobile)obj;		

			  if ( mob.IsDeadBondedPet )
			  {
				BaseCreature bc = mob as BaseCreature;
				{	
								if(bc.ControlMaster == m)
								{
										m.CloseGump( typeof( PetResurrectGump ) );
										m.SendGump( new PetResurrectGump( m, bc ) );
                                        
								}
								else
								{
									m.SendMessage( "that is not your pet." );
								}
				}	
				}
								else
								{
									m.SendMessage( "that pet is not dead." );
								}
			}
 		}
 	}
 }
 } 

